package com.ruoyi.web.controller.community;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.CommunityPost;
import com.ruoyi.community.service.ICommunityPostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 社区帖子Controller
 *
 * @author FeiYiLand
 * @date 2024-07-14
 */
@Anonymous
@RestController
@RequestMapping("/community/post")
public class CommunityPostController extends BaseController
{
    @Autowired
    private ICommunityPostService communityPostService;

    /**
     * 查询社区帖子列表
     */
//    @PreAuthorize("@ss.hasPermi('community:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityPost communityPost, SysUser user)
    {
        startPage();
        List<CommunityPost> list = communityPostService.selectCommunityPostList(communityPost);
        return getDataTable(list);
    }

    /**
     * 导出社区帖子列表
     */
//    @PreAuthorize("@ss.hasPermi('community:post:export')")
    @Log(title = "社区帖子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityPost communityPost)
    {
        List<CommunityPost> list = communityPostService.selectCommunityPostList(communityPost);
        ExcelUtil<CommunityPost> util = new ExcelUtil<CommunityPost>(CommunityPost.class);
        util.exportExcel(response, list, "社区帖子数据");
    }

    /**
     * 获取社区帖子详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(communityPostService.selectCommunityPostByPostId(postId));
    }

    /**
     * 新增社区帖子
     */
//    @PreAuthorize("@ss.hasPermi('community:post:add')")
    @Log(title = "社区帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityPost communityPost)
    {
        return toAjax(communityPostService.insertCommunityPost(communityPost));
    }

    /**
     * 修改社区帖子
     */
//    @PreAuthorize("@ss.hasPermi('community:post:edit')")
    @Log(title = "社区帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunityPost communityPost)
    {
        return toAjax(communityPostService.updateCommunityPost(communityPost));
    }

    /**
     * 删除社区帖子
     */
//    @PreAuthorize("@ss.hasPermi('community:post:remove')")
    @Log(title = "社区帖子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(communityPostService.deleteCommunityPostByPostIds(postIds));
    }
}
